﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using HHL_Acc.HHL_DaoAcc.DataBase;
using HHL_Acc.HHL_EntityAcc;

namespace HHL_Acc.HHL_DaoAcc.DataObject
{
    public class GenLoginsDao : CBaseDao
    {
        public GenLogins GetGenLoginByAccount(String account)
        {
            String sql =
                "select l.Login,l.EmpId,l.Password, l.Disabled,l.TeamId,l.StationId, (e.LASTNAME+' ' +e.FIRSTNAME) as Name from Gen_Logins l inner join Gen_Employees e on  l.EmpId = e.EMPID where l.login = @account";
            DataSet ds = ExcuteSql(sql, CommandType.Text, new SqlParameter("@account", account));
            if (ds != null)
                return CDb.Map<GenLogins>(ds.Tables[0]);
            return null;
        }

        // Lấy về danh sách những nhóm 
        public List<String> GetGroupID(String strUsername)
        {
            string sql = @"Select GRPID from GEN_GROUP_LOGIN Where Login='" + strUsername + "' AND Disabled=0";
            DataSet ds = ExcuteSql(sql);
            DataTable data = new DataTable();
            data = ds.Tables[0];
            List<String> list = new List<String>();
            if (ds != null && data.Rows.Count > 0)
            {
                foreach (DataRow row in data.Rows)
                {
                    list.Add(row["GRPID"].ToString());
                }
            }
            return list;
        }

        //Lấy về danh sách chức năng của user đó
        public List<String> GetFunctions(List<String> group)
        {
            string subSql = "";
            string sql = @"Select distinct FuncID from GEN_GROUP_FUNCTION Where GRPID in(";
            foreach (String li in group)
            {
                if (subSql == "")
                {
                    subSql = "'" + li + "'";
                }
                else
                {
                    subSql = subSql + ",'" + li + "'";
                }
            }
            sql = sql + subSql + ")";
            DataSet ds = ExcuteSql(sql);
            DataTable data = new DataTable();
            data = ds.Tables[0];
            List<String> list = new List<String>();
            if (ds != null && data.Rows.Count > 0)
            {
                foreach (DataRow row in data.Rows)
                {
                    list.Add(row["FUNCID"].ToString());
                }
            }
            return list;
        }

        //Lấy danh sách có form có quyền của user login đó
        public List<String> GetObjects(List<String> group)
        {
            string subSql = "";
            string sql = @"Select distinct OBJID from GEN_FUNCTION_OBJECT Where FUNCID in(";
            foreach (String li in group)
            {
                if (subSql == "")
                {
                    subSql = "'" + li + "'";
                }
                else
                {
                    subSql = subSql + ",'" + li + "'";
                }
            }
            sql = sql + subSql + ")";
            DataSet ds = ExcuteSql(sql);
            DataTable data = new DataTable();
            data = ds.Tables[0];
            List<String> list = new List<String>();
            if (ds != null && data.Rows.Count > 0)
            {
                foreach (DataRow row in data.Rows)
                {
                    list.Add(row["OBJID"].ToString());
                }
            }
            return list;
        }
    }
}